package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ICustomerService customerService = new CustomerServiceImpl();
        //        1、接收前端传过来的参数
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String zipCode = req.getParameter("zipCode");
        String address = req.getParameter("address");
        String telephone = req.getParameter("telephone");
        String email = req.getParameter("email");
        //2、接收其他参数
//        ........填空（一）
        Customer customer =
                new Customer(name,password,zipCode,address,telephone,email);
//        customer.setName(name);
//        3、调用对应的set方法 把接收到的参数set到对象当中
//        ....填空（二）
        //调用对应方法
        HttpSession session = req.getSession();
        try {
            customerService.register(customer);
            //...填空：跳转到登录页
            resp.sendRedirect("login.jsp");
        }catch (Exception e){
            e.printStackTrace();
            String message = e.getMessage();
            session.setAttribute("error",message);
            resp.sendRedirect("register.jsp");
            //...填空：跳转到注册页
        }
    }
}